package com.yiwei.jmx.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Spanned;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yiwei.jmx.R;
import com.yiwei.jmx.api.model.BankCardListModel;
import com.yiwei.jmx.util.Spans;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.yiwei.jmx.R.dimen.x30;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class BankSelectDialogAdapter extends ItemAdapter<BankCardListModel.DataBean, BankSelectDialogAdapter.DefaultHolder> {

    private final int x26;
    private Context context;

    public BankSelectDialogAdapter(Context context) {
        super(context);
        this.context = context;
        x26 = context.getResources().getDimensionPixelSize(R.dimen.x26);
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_bank_select_dialog, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        BankCardListModel.DataBean item = getItem(position);
        Spanned bank_info = Spans.with(context).font(item.bank_name, x26, 0xff333333).
                font(item.last_num, x26, 0xff999999).
                build();
        holder.txtBankInfo.setText(bank_info);
        Glide.with(context).load(item.icon).into(holder.imgIcon);
        holder.layoutTotal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.btnCheck.setImageResource(R.mipmap.img_check_select);
                holder.txtBankInfo.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (onChooseListener != null) {
                            onChooseListener.onSuccess(item);
                        }
                    }
                },200);
//                notifyDataSetChanged();
            }
        });
    }

    class DefaultHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_icon)
        ImageView imgIcon;
        @BindView(R.id.txt_bank_info)
        TextView txtBankInfo;
        @BindView(R.id.btn_check)
        ImageView btnCheck;
        @BindView(R.id.layout_total)
        FrameLayout layoutTotal;

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onSuccess(BankCardListModel.DataBean item);
    }
}
